/* eslint-disable import/no-anonymous-default-export */
import { Dispatch } from 'redux'

// 定义State
type State = Readonly<{
  appsList: any[] | undefined
}>

type Action = {
  type: string
  payload: any
}
//定义Dispatch
export const getAppList = (param:any,callBack:()=>void) => {
  console.log('getAppList', param)
  return (dispatch:Dispatch) => {
    // ajax
    setTimeout(() => {
      const appList = [
        {
          appName:'test1',
          appId: 'id1'
        },
        {
          appName:'test2',
          appId: 'id2'
        },
      ]
      Promise.resolve(appList)
      .then(res => {
        dispatch({
          type: "GET_APPSLIST",
          payload: res
        })
      })
      callBack()
    }, 1000)
  }
}

const initState:State = {
  appsList: undefined
}
export default function(state=initState, action:Action) {
  switch (action.type) {
    case 'GET_APPSLIST':
      return {
        ...state,
        appsList: action.payload
      }
    default:
      return state;
  }
}